package main

import (
	"github.com/micro/go-micro/v2"
	"github.com/micro/go-micro/v2/registry"
	"github.com/micro/go-plugins/registry/consul/v2"
	"gomicrodemo/proto/rand"
	"gomicrodemo/rand_service/handler"
	"gomicrodemo/rand_service/middleware"
)

func main() {
	//声明consul服务发现的端口号
	consulRegistry := consul.NewRegistry(
		registry.Addrs("127.0.0.1:8500"))

	//初始化sential限流器和配置限流规则
	middleware.InitSential()

	service := micro.NewService(
		micro.Name("go.micro.homin.rand"),
		micro.Registry(consulRegistry),                   //将服务注册到consul
		micro.WrapHandler(middleware.LimitFlowWrapper()), //将自定义包装器对service进行封装
	)

	//服务初始化
	service.Init()

	//暴露接口到对应的服务，注册服务端，传入服务对象
	rand.RegisterRandHandler(service.Server(), handler.NewRandHandler())

	if err := service.Run(); err != nil {
		panic(err)
	}
}
